<?php
 namespace home\model;
 use \core\Model;

 class ArticleModel extends Model{
     //属性
     protected $table = 'article';
     //根据分页获取博文信息
     public function getAllrticles($cond=array(),$pagecond=5,$page=1){
         //计算分页信息
         $offset=($page - 1 ) * $pagecond;
         //组织条件
         $where= ' where a_is_delete = 0 and a_status = 0';
         //条件查询
         if (isset($cond['a_title'])) $where.=" and a_title like '%{$cond['a_title']}%' ";
         if (isset($cond['c_id'])) $where.=" and c_id = {$cond['c_id']} ";
         //组织完整的sql指令
         $sql = "select a.*,c.c_count from {$this->getTable()} a left join (select a_id,count(*) c_count from {$this->getTable('comment')} group by a_id) c on a.id = c.a_id {$where} limit {$offset},{$pagecond}";
         return $this->query($sql,true);
     }
     //根据分类获取博文数量
     public function getCountsByCategory(){
         //组织sql
         $sql="select c_id,count(*) c from {$this->getTable()} where a_is_delete = 0 and a_status = 2 group by c_id";
         //执行
         $res = $this->query($sql,true);
            //加工分类
         $list=array();
         foreach ($res as $v){
             $list[$v['c_id']]=$v['c'];
         }
         //返回结果
         return $list;
     }
     //获取三条最基本的信息
     public function getNewsInfo($limit=3)
     {
         //组织sql
         $sql="select id ,a_title,a_img_thumb from {$this->getTable()} where a_is_delete = 0 order by a_time desc limit {$limit}";
         return $this->query($sql,true);
     }
     //根据条件获取记录数
     public function getCounts($cond=array())
     {
         //组织条件
         $where= ' where a_is_delete = 0 and a_status = 0';
         //条件查询
         if (isset($cond['a_title'])) $where.=" and a_title like '%{$cond['a_title']}%' ";
         if (isset($cond['c_id'])) $where.=" and c_id = {$cond['c_id']} ";
         //组织完整sql
         $sql = "select count(*) c from {$this->getTable()} {$where}";
         $res=$this->query($sql);
         return $res['c'] ?? 0;
     }
 }